﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using CMSMVC4.Areas.Main.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Main.Controllers
{
    public class TinTucController : Controller
    {
        CommandSql cm = new CommandSql();
        CMSMVC4Entities db = new CMSMVC4Entities();
        //
        // GET: /Main/TinTuc/

        [OutputCache(Duration = 900, VaryByParam = "id,page", Location = OutputCacheLocation.Client, NoStore = true)]
        public ActionResult Index(string id,string page)
        {
            ViewBag.Title = MultiResource.Resource.Title_News;
            ViewBag.key = "Tin tức công nghệ 3dscan việt nam";
            ViewBag.description = "Những bản tin công nghệ liên quan tới công nghệ 3dscan việt nam";
            Category data;
            int i;
            if (int.TryParse(id,out i))
            {
                data = db.Categories.FirstOrDefault(x => x.Id == i);
            }
            else
            {
                data = db.Categories.OrderByDescending(x=>x.Sort).FirstOrDefault(x => x.Type == MenuType.TinTuc && x.Status == CategoriStatus.Thuong);
            }
            if (data != null)
            {
                ViewBag.Id = data.Id;
                string cuture = cm.GetLanguage();
                var row = db.CateLangs.FirstOrDefault(x => x.CategoriesId == data.Id && x.LanguageCode == cuture);
                int count = cm.SetCountTinKhac(20,data.Id);
                ViewBag.tinkhac = cm.GetDataTinKhac(ref page, data.Id);
                ViewBag.count = count;
                ViewBag.page = page;//phải dưới ham getdata
            }
            else
            {
                data = db.Categories.OrderByDescending(x => x.Id).FirstOrDefault(x => x.Type == MenuType.TinTuc);
                ViewBag.Id = data.Id;
                string cuture = cm.GetLanguage();
                var row = db.CateLangs.FirstOrDefault(x => x.CategoriesId == data.Id && x.LanguageCode == cuture);
                int count = cm.SetCountTinKhac(20, data.Id);
                ViewBag.tinkhac = cm.GetDataTinKhac(ref page, data.Id);
                ViewBag.count = count;
                ViewBag.page = page;//phải dưới ham getdata
            }
            //select menu dang chon
            var menu = db.Menus.FirstOrDefault(x => x.Type == MenuType.TinTuc);
            if (menu != null)
            {
                ViewBag.Menu = menu.Id;
            }
            else
            {
                ViewBag.Menu = 0;
            }
            return View();
        }

        [ChildActionOnly]
        public ActionResult ListTinXemNhiew(int idcate)
        {
            var data = cm.GetBaiVietXemNhieuNhat(idcate);
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult ListNewsHot()
        {
            var data = cm.GetNewsHot();
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult ListTinTuc()
        {
            var data = cm.GetNameCategories(MenuType.TinTuc);
            return PartialView(data);
        }

        [OutputCache(Duration = 900, VaryByParam = "id", Location = OutputCacheLocation.Client, NoStore = true)]
        public ActionResult BaiViet(BaiVietModel model)
        {
            if (ModelState.IsValid)
            {
                string culture = cm.GetLanguage();
                var data = db.PageLangs.FirstOrDefault(x => x.PageId == model.id && x.LanguageCode.Equals(culture));
                if (data != null)
                {
                    var page = db.Pages.FirstOrDefault(x => x.Id == data.PageId);
                    if (page != null) page.View = page.View + 1;
                    db.SaveChanges();
                    ViewBag.Id = data.Page.CategoriesID;
                    ViewBag.Title = data.Name;
                    ViewBag.IdPage = data.PageId;
                    ViewBag.key = data.Name;
                    ViewBag.description = data.Summary;
                }
                //select menu dang chon
                var menu = db.Menus.FirstOrDefault(x => x.Type == MenuType.TinTuc);
                if (menu != null)
                {
                    ViewBag.Menu = menu.Id;
                }
                else
                {
                    ViewBag.Menu = 0;
                }
                return View(data);
            }
            return View("Error");
        }

    }
}
